﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WebTranslator;
namespace TranslationClient {
	public partial class KlientWT : Form {
		CMSProtocolC clientProtocol = new CMSProtocolC();
		public KlientWT() {
			InitializeComponent();
			blokada(false);
		}

		void blokada(bool enabled) {
			var B = new List<Control>();
			B.Add(buttonDodajTlumaczenie);
			B.Add(buttonZaloguj);
			B.Add(buttonZostanTlumaczem);
			B.Add(buttonZleconeTlumaczenia);
			B.Add(buttonZarejestruj);
			B.Add(textBoxLogin);
			B.Add(textBoxPassword);
			foreach (Control b in B) b.Enabled = enabled;
			List<string> S = new List<string>();
			foreach (var s in StartingApplicationPorts.MyIpAddress) if (IsIPv4(s.ToString())) S.Add(s.ToString());

			comboBox1.DataSource = S;
		}

		private void button1_Click(object sender, EventArgs e) {
			DodawanieTlumaczenia okno = new DodawanieTlumaczenia(clientProtocol);
			okno.ShowDialog();
		}

		private void button6_Click(object sender, EventArgs e) {
			if (clientProtocol.Connect(textBoxIp.Text)) {
				textBoxLogin.Enabled = true;
				buttonZaloguj.Enabled = true;
				textBoxPassword.Enabled = true;
				buttonZarejestruj.Enabled = true;
				buttonDodajTlumaczenie.Enabled = false;
				buttonZleconeTlumaczenia.Enabled = false;
				buttonZostanTlumaczem.Enabled = false;
			} else {
				MessageBox.Show("Nie udało się połączyć z serwerem");
			}
		}

		private void button2_Click(object sender, EventArgs e) {
			string msg = clientProtocol.Login(textBoxLogin.Text, textBoxPassword.Text);
			if (!msg.StartsWith("OK"))
				MessageBox.Show(msg);
			else {
				labelLogin.Text = msg.Substring(3);
				try {
					clientProtocol.ConnectToLanguageServer();
					MessageBox.Show("Zalogowany");
					labelIloscPunktow.Text = clientProtocol.IlePunktow();
					textBoxLogin.Enabled = false;
					buttonZaloguj.Enabled = false;
					textBoxPassword.Enabled = false;
					buttonZarejestruj.Enabled = false;
					buttonDodajTlumaczenie.Enabled = true;
					buttonZleconeTlumaczenia.Enabled = true;
					buttonZostanTlumaczem.Enabled = true;

				} catch (ServerUnavailableException ex) {
					MessageBox.Show(ex.Message);
				}
			}
		}

		private void button5_Click(object sender, EventArgs e) {
			new Rejestracja(clientProtocol).Show();
		}

		private void button3_Click(object sender, EventArgs e) {
			new WykonanieTlumaczenia(clientProtocol, this).ShowDialog();
		}

		private void buttonZleconeTlumaczenia_Click(object sender, EventArgs e) {
			new ZleconeTlumaczenia(clientProtocol).ShowDialog();
		}

		private void KlientWT_FormClosing(object sender, FormClosingEventArgs e) {
			clientProtocol.Send("quit");
		}

		bool IsIPv4(string s) {
			return !s.Contains(':');
		}

		private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) {
			string wyn = comboBox1.Text;
			textBoxIp.Text = "";
			int i = 0, kr = 0;
			while (kr < 3) {
				textBoxIp.Text += wyn[i];
				if (wyn[i] == '.') kr++;
				i++;

			}
			textBoxIp.Update();
		}

		public void setPoint(string x) {
			this.labelIloscPunktow.Text = x;
		}
	}


}
